﻿
#If Not PocketPC AndAlso Not Smartphone Then
Imports System.IO
Imports System.Threading
Imports System.Drawing

Public Class webBrowserDrawer

    Private drawer As webBrowserDrawer2 = Nothing
    Private thread As Thread = Nothing

    Private Delegate Sub showDelegate()
    Private Delegate Sub hideDelegate()
    Private Delegate Function saveStreamDelegate(ByVal url As String, ByRef stream As Stream _
                                                 , ByVal format As Imaging.ImageFormat _
                                                 , ByVal size As Size, ByVal destinationSize As Size) As Boolean
    Private Delegate Function saveFileDelegate(ByVal url As String, ByVal filename As String _
                                               , ByVal format As Imaging.ImageFormat _
                                               , ByVal size As Size, ByVal destinationSize As Size) As Boolean

    Public Function busy() As Boolean
        assert(Not drawer Is Nothing, "drawer is nothing.")
        Return drawer.busy()
    End Function

    Public Sub show()
        drawer.Invoke(New showDelegate(AddressOf drawer.Show))
    End Sub

    Public Sub hide()
        drawer.Invoke(New hideDelegate(AddressOf drawer.Hide))
    End Sub

    Public Function save(ByVal url As String, ByRef stream As Stream, ByVal format As Imaging.ImageFormat _
                         , ByVal size As Size, ByVal destinationSize As Size) As Boolean
        Dim parameters() As Object = {url, stream, format, size, destinationSize}
        Dim rtn As Boolean = False
        Try
            rtn = drawer.Invoke(New saveStreamDelegate(AddressOf drawer.save), parameters)
        Catch ex As Exception
            raiseError("cannot invoke drawer, " + ex.Message, errorHandle.errorType.warning)
        End Try
        stream = parameters(1)

        Return rtn
    End Function

    Public Function save(ByVal url As String, ByVal filename As String, ByVal format As Imaging.ImageFormat _
                         , ByVal size As Size, ByVal destinationSize As Size) As Boolean
        Dim parameters() As Object = {url, filename, format, size, destinationSize}
        Dim rtn As Boolean = False
        Try
            rtn = drawer.Invoke(New saveStreamDelegate(AddressOf drawer.save), parameters)
        Catch ex As Exception
            raiseError("cannot invoke drawer, " + ex.Message, errorHandle.errorType.warning)
        End Try

        Return rtn
    End Function

    Public Function save(ByVal url As String, ByRef stream As Stream _
                         , ByVal size As Size, ByVal destinationSize As Size) As Boolean
        Return save(url, stream, Imaging.ImageFormat.Jpeg, size, destinationSize)
    End Function

    Public Function save(ByVal url As String, ByVal fileName As String _
                         , ByVal size As Size, ByVal destinationSize As Size) As Boolean
        Return save(url, fileName, Imaging.ImageFormat.Jpeg, size, destinationSize)
    End Function

    Public Function save(ByVal url As String, ByRef stream As Stream _
                         , Optional ByVal width As Int64 = npos, Optional ByVal height As Int64 = npos _
                         , Optional ByVal destinationWidth As Int64 = npos _
                         , Optional ByVal destinationHeight As Int64 = npos) As Boolean
        Return save(url, stream, webBrowserDrawer2.generateSize(width, height) _
                    , webBrowserDrawer2.generateSize(destinationWidth, destinationHeight))
    End Function

    Public Function save(ByVal url As String, ByVal filename As String _
                         , Optional ByVal width As Int64 = npos, Optional ByVal height As Int64 = npos _
                         , Optional ByVal destinationWidth As Int64 = npos _
                         , Optional ByVal destinationHeight As Int64 = npos) As Boolean
        Return save(url, filename, webBrowserDrawer2.generateSize(width, height) _
                    , webBrowserDrawer2.generateSize(destinationWidth, destinationHeight))
    End Function

    Protected Sub drawerThread()
        Try
            drawer = New webBrowserDrawer2
            Windows.Forms.Application.Run(drawer)
        Catch ex As ThreadAbortException
            Windows.Forms.Application.Exit()
        Finally
            'While Windows.Forms.Application.MessageLoop()
            '    Windows.Forms.Application.DoEvents()
            'End While
        End Try
    End Sub

    Public Sub New()
        thread = New Thread(AddressOf drawerThread)
        thread.TrySetApartmentState(ApartmentState.STA)
        thread.Start()
        While drawer Is Nothing OrElse Not drawer.IsHandleCreated()
            sleep(64)
        End While
    End Sub

    Public Sub close()
        thread.Abort()
        thread.Join()
    End Sub
End Class
#End If
